<div style="margin: 15px;">
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">授权服务</label>

            <div class="layui-input-block">
                <select id="service" class=" form-control " multiple="multiple">
                </select>
            </div>
        </div>

        <button lay-filter="edit" lay-submit style="display: none;"></button>
    </form>
</div>

<script type="text/javascript">
    $("#service").select2({
        ajax: {
            url: "/element/page",
            dataType: 'json',
            delay: 250,
            data: function (params) {
                return {
                    name: params.term, // search term
                    menuId: -1,
                    offset: params.page
                };
            },
            processResults: function (data, params) {
                params.page = params.page || 1;
                var result = [];
                for (var i = 0; i < data.rows.length; i++) {
                    var obj = {};
                    obj.id = data.rows[i].id;
                    obj.text = data.rows[i].name;
                    obj.uri = data.rows[i].uri;
                    obj.method = data.rows[i].method;
                    result.push(obj);
                }
                return {
                    results: result,
                    pagination: {
                        more: (params.page * 10) < data.total
                    }
                };
            },
            cache: true
        },
        minimumInputLength: 1,
        language: "zh-CN", //设置 提示语言
//            maximumSelectionLength: 3,  //设置最多可以选择多少项
        placeholder: "请选择",
        tags: false, //设置必须存在的选项 才能选中
        templateResult: function (item) {
            return '<span >' + item.text + '</span><span style="font-size:8px"> : ' + item.uri + '</span><span style="font-size:8px"> : ' + item.method + '</span>'
        },
//        templateSelection: function (item) {
//            return item.name
//        },
        escapeMarkup: function (markup) {
            return markup;
        },
    });
</script>
